import urllib.request
import urllib.parse
from http.client import HTTPResponse

"""
    需求：获取豆瓣电影前100名
"""


def create_request(start, headers):
    # 1.创建请求对象，编码
    url = f"https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start={i * 20}&limit=20"
    return urllib.request.Request(url, headers=headers)


def getcontent(request):
    response = urllib.request.urlopen(request)
    return response.read().decode("utf-8")


if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
    }
    for i in range(10):
        r = create_request(i * 20, headers)
        content = getcontent(r)
        print(content)
